from multiprocessing import Pool
import os,time,random

def long_time_task(name):
    print('运行任务 %s (%s)' % (name,os.getpid()))
    start = time.time()
    time.sleep(random.random * 3)
    end = time.time()
    print('任务 %s 运行了 %s 秒' % (name,(end - start)))

if __name__=='__main__':
    print('父进程是%s' % os.getpid())
    p = Pool(4)
    for i in range(5):
        p.apply_async(long_time_task,args=(i,))
    print("等待所有子进程执行完毕")
    p.close()
    p.join()
    print("所有进程执行完毕")